package 力扣日常刷题.木23一月.第06天306;

import java.util.*;

/**
 * @author 帅小伙
 * @date 2022/3/6
 * @description
 */
public class Demo56字母异位词分组 {

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("abc");
        strings.add("cba");
        groupAnagrams(strings);
    }

    public static List<List<String>> groupAnagrams(List<String> strs) {
        Map<String, List<String>> map = new HashMap<>();

        for (String str : strs) {
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            List<String> strings = map.getOrDefault(key, new ArrayList<String>());
            strings.add(str);
            map.put(key, strings);
        }
        return new ArrayList<List<String>>(map.values());

    }

}
